//
//  @ Project : Galaxy Star Trading System
//  @ File Name : TradeStrategySessionManager.h
//  @ Date : 2014/9/5
//  @ Author :
//
//


#if !defined(_CTRADESTRATEGYSESSIONMANAGER_H)
#define _CTRADESTRATEGYSESSIONMANAGER_H

namespace GSTrader
{
	namespace TradeStrategy
	{
		class TRADE_STRATEGY_API  CStrategySessionManager
		{
		public:
			CStrategySessionManager(CFramework* pFramework);
			virtual ~CStrategySessionManager();

			CStrategySession* GetStrategySession(int nID);
			CStrategyBase* GetStrategyById(int nStrategyId);
			void StartStrategy(CStrategySession* pStrategySession);
			void StopStragegy(CStrategySession* pStrategySession);
			void Clear();

		public:
			void OnNewOrder(COrder* pOrder);
			void OnOrderStatusChanged(COrder* pOrder, int nOldStatus);
			void OnOrderFailed(COrder* pOrder);
			void OnOrderError(COrder* pOrder);
			void OnOrderPartTraded(COrder* pOrder);
			void OnOrderTraded(COrder* pOrder);
			void OnOrderDone(COrder* pOrder);
			void OnOrderCanceled(COrder* pOrder);
			void OnCancelOrderFailed(COrder* pOrder);
			void OnPendingNewOrder(COrder* pOrder);
			void OnCancelOrder(COrder* pOrder);

			void OnNewReplaceOrder(CReplaceOrder* pReplaceOrder);
			void OnReplaceOrderCanceled(COrder* pOrder);
			void OnReplaceSendNewOrder(COrder* pOrder);

			void OnExecutionReport(CExecutionReport* pReport);
		
			void OnTradeConnected(CAccountData* pConnInfo);
			void OnMarketDataConnected(CAccountData* pConnInfo);
			void OnTradeDisconnected(CAccountData* pAccountData);
			void OnMarketDataDisconnected(CAccountData* pAccountData);

			void OnMarketData(CDepthMarketData* pDepthMarketData);
			void OnBar(CBarData* pBar);
			void OnBarOpen(CBarData* pBar);
	
			void OnInstrumentBegin(int nProviderId);
			void OnInstrumentEnd(int nProviderId);
			void OnAddSpreadInstument(CSpreadInstrument* pInstrument);

			void OnSendOrder(COrder* pOrder);
			void OnPositionOpen(CPortfolioPosition* pPosition);
			void OnPositionChange(CPortfolioPosition* pPosition);
			void OnPositionClose(CPortfolioPosition* pPosition);
			
			void OnExchangeStatusChange(CExchangeStatus* pExchangeStatus);
			void OnTrade(COrder* pOrder, CTrade* pTrade);

			void OnError(CErrorMsg* pError);
			void OnRspPosition(CPosition* pPosition);

			void SyncPosition(int nProviderId);

			void OnRemoveProvider(CConnectionProvider* pProvider);
		public:
			CStrategyBase* GetStrategyByName(string strStrategyName);

		private:
			bool AddStrategySession(CStrategySession* pSession);
			bool RemoveStrategySession(CStrategySession* pSession);
			
		private:
			CFramework* m_pFramework;
			atomic<int> m_nNextID;
			map<int, CStrategySession*> m_mapStrategySession; 
	
			friend class CStrategySession;
		};
	}
}

#endif //_CTRADESTRATEGYSESSIONMANAGER_H